package com.miyake.loveviewweb.client.explorer;

import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.client.service.DataServiceAsync;
import com.miyake.loveviewweb.shared.model.ChartModel;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.FolderModel;

public class ExplorerBuilder {
    private DataServiceAsync service = DataService.Util.getInstance();
    private ExplorerBuilderListener explorer;
    public ExplorerBuilder(ExplorerBuilderListener explorer) {
        this.explorer = explorer;
        
    }
    
    protected void getRootFolders() {
        service.getRootFolders(new AsyncCallback<List<FolderModel>>() {

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(List<FolderModel> result) {
                for (FolderModel rootFolder : result) {
                    explorer.addRootFolder(rootFolder);
                    getSubFolders(rootFolder.getKey());
                    getData(rootFolder.getKey());
                }
            }
            
        });  
    }
    
    protected void getData(final Key folderKey) {
        service.getDataList(folderKey, false, new AsyncCallback<List<DataModel>>() {
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(List<DataModel> result) {
                for (DataModel data : result) {
                    if (data.getSourceData() == null) {
                        explorer.addData(folderKey, data);
                        getCharts(data.getKey());
                        getSubData(data.getKey());
                        
                    }
                }
            }
        });
        
    }

    protected void getSubData(final Key dataKey) {
        service.getSubDataList(dataKey, false, new AsyncCallback<List<DataModel>>() {
            public void onFailure(Throwable caught) {
            }

            public void onSuccess(List<DataModel> result) {
                for (DataModel data : result) {
                    explorer.addData(dataKey, data);
                }
            }
        });  
    }
    
    protected void getSubFolders(final Key key) {
        service.getSubFolders(key, new AsyncCallback<List<FolderModel>>() {

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(List<FolderModel> result) {
                for (FolderModel folder : result) {
                    explorer.addFolder(key, folder);
                    getSubFolders(folder.getKey());
                    getData(folder.getKey());
                }
            }
            
        });
    }

    protected void getCharts(final Key key) {
        service.getChartList(key, false, new AsyncCallback<List<ChartModel>>() {
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(List<ChartModel> result) {
                for (ChartModel chart : result) {
                    explorer.addChart(key, chart);
                }
            }

            
        });
    }
    
    public void start() {
        this.getRootFolders();
    }

    public void start(Key key) {
        
        if (FolderModel.class.getName().contains(key.getKind())) {
            getSubFolders(key);
            getData(key);
        }
        else if (DataModel.class.getName().contains(key.getKind())) {
            getSubData(key);
        }
    }
}
